import { login } from '@/api/user'
import router from '@/router'
import { delToken, getToken, setToken } from '@/utils/storage'

export default {
  namespaced: true,
  state () {
    return {
      token: getToken()
    }
  },
  mutations: {
    setUserToken (state, token) {
      state.token = token
      setToken(token)
    },
    delUserToken (state) {
      state.token = ''
      delToken()
    }
  },
  actions: {
    async loginAction (context, obj) {
      const res = await login(obj)
      console.log(res)
      context.commit('setUserToken', res.data.token)
      router.push('/')
    }
  }
}
